/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.servlet;

import br.com.foxline.service.DneService;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author roniere
 */
@WebServlet(name = "dneServlet", urlPatterns = "/dneServlet/*")
public class DneServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;
    @EJB
    DneService dneService;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestData = processRequest(req);
        Date date = null;
        OutputStream outs;
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss.SSS");
        Gson gson = new GsonBuilder().setDateFormat("dd/MM/yyyy HH:mm:ss.SSS").create();
        resp.setContentType("application/json");

        try {
            date = sdf.parse(requestData);
        } catch (ParseException ex) {
            Logger.getLogger(DneServlet.class.getName()).log(Level.SEVERE, null, ex);
        }

        PrintWriter out = resp.getWriter();
        out.write(gson.toJson(dneService.getAllUpdate(date)));
        out.flush();

    }

    private String processRequest(HttpServletRequest request) {
        StringBuffer jb = new StringBuffer();
        String line = null;
        try {
            BufferedReader reader = request.getReader();
            while ((line = reader.readLine()) != null) {
                jb.append(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return jb.toString();

    }
}
